package com.ams.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @className BasicAction
 * @description Gets the object associated servletApi
 * @version $Revision: 4.0 $
 * @author {WU CHUNTAO }.
 * 
 */

public class BasicAction extends ActionSupport implements ServletRequestAware,
		SessionAware, RequestAware, ServletResponseAware {

	private HttpServletRequest httpRequest = null;
	private HttpServletResponse httpResponse = null;
	private Map<String, Object> request = null;
	private Map<String, Object> session;
	
	public BasicAction(){
	}
	

	/**
	 * Incoming Map object
	 * 
	 * @param request
	 *            Incoming Map object
	 */
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	/**
	 * Incoming HttpServletRequest object
	 * 
	 * @param request
	 *            Incoming HttpServletRequest object
	 */
	public void setServletRequest(HttpServletRequest req) {
		this.httpRequest = req;
	}

	/**
	 * Gets the instance of the HttpServletRequest
	 * 
	 * @return httpRequest the single instance of the HttpServletRequest.
	 */
	public HttpServletRequest getServletRequest() {
		return httpRequest;
	}

	/**
	 * Gets the instance of the Map
	 * 
	 * @return request the single instance of the Map.
	 */
	public Map<String, Object> getRequest() {
		return request;
	}

	/**
	 * Incoming Map object
	 * 
	 * @param session
	 *            Incoming Map object
	 */
	public void setSession(Map<String, Object> ses) {
		this.session = ses;

	}

	/**
	 * Gets the instance of the Map
	 * 
	 * @return session the single instance of the Map.
	 */
	public Map<String, Object> getSession() {
		return this.session;
	}

	/**
	 * Gets the instance of the HttpSession
	 * 
	 * @return the single instance of the HttpSession.
	 */
	public HttpSession getHttpSession() {
		return httpRequest.getSession(true);
	}

	/**
	 * Incoming HttpServletResponse object
	 * 
	 * @param httpResponse
	 *            Incoming HttpServletResponse object
	 */
	public void setServletResponse(HttpServletResponse res) {

		this.httpResponse = res;
	}

	/**
	 * Gets the instance of the HttpServletResponse
	 * 
	 * @return the single instance of the HttpServletResponse.
	 */
	public HttpServletResponse getHttpResponse() {
		return httpResponse;
	}
	
	public void gotoLoginPage(boolean isAlert)throws IOException{
		HttpSession ses = getHttpSession();
		HttpServletRequest req = getServletRequest();
		String path = req.getScheme() + "://" + req.getServerName() + ":"
				+ req.getServerPort() + req.getContextPath() + "/";
		//System.out.println("-====="+path);
		ses.invalidate();   //Log off the current user 
		PrintWriter out = getHttpResponse().getWriter();
		String script="<script>window.parent.location.href='" + path + "login.jsp"
		+ "';</script>";
		if(isAlert){
			script="<script>alert('您长时间没有操作，请重新登录');window.parent.location.href='" + path + "login.jsp"
			+ "';</script>";
		}
		out.print(script);//Jump to login page 
		out.flush();
		out.close();
	}
	

}
